import { _decorator, Component, Node, tween } from 'cc';
import { TankShoot } from './TankShoot';
import { TankMove } from './TankMove';
import { DIR } from '../Common';
import { Random } from '../../mathAlgo/Random';
const { ccclass, property } = _decorator;
import { GameMgr } from '../GameMgr';

@ccclass('TankAI')
export class TankAI extends Component {
    tShoot:TankShoot|null=null;
    tMove:TankMove|null=null;

    
    moveTimer:number=0;
    moveCD:number=2;

    turnDirTimer:number=0;
    turnDirCD:number=1;


    start() {
        this.tMove=this.getComponent(TankMove);
        this.tMove.changeMoveState(false);
        this.scheduleOnce(()=>{
        this.tMove.changeMoveState(true);
        },0.5);
        this.tShoot=this.getComponent(TankShoot)

        this.tMove.changeDir(this.getRandomDir());

        this.schedule(this.autoShoot,Random.Range2(2,3),100000,1);


        GameMgr.Inst.addEnemyAI(this);
    }

    protected onDestroy(): void {
        GameMgr.Inst. removeEnemyAI(this);
    }

    autoShoot(){
       // console.log("autoshoot");
        this.tShoot.shoot();
    }

    //停止所有活动
    stopAllExe(isStop:boolean){

        if(isStop){
            this.tMove.enabled=false;
            this.enabled=false;
        }else {
            //这个没有记录之前的状态，但是不影响，
            this.tMove.enabled=true;
            this.enabled=true;
        }
    }

    getRandomDir(){
        let data=[DIR.left,DIR.right,DIR.top,DIR.bottom];
        return Random.RandomItem(data);
    }

    update(deltaTime: number) {


        this.turnDirTimer+=deltaTime;
        if(this.turnDirTimer>this.turnDirCD){
            this.tMove.changeDir(this.getRandomDir());
            this.turnDirCD=Random.Range2(2,3);
            
            this.turnDirTimer=0;
        }

        this.moveTimer+=deltaTime;
        if(this.moveTimer>this.moveCD){
           //this.tMove.isMove=!this.tMove.isMove;
           this.tMove.changeMoveState(!this.tMove.isMove)
            this.moveCD=Random.Range2(1,2);

            this.moveTimer=0;
        }
        
    }
}


